package com.topvision.elementanim.hualang;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.topvision.elementanim.R;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class ViewPagerActivity extends AppCompatActivity implements ItemClickedInterface, PageChangeInterface {
    private String IMAGE_FLORD = Environment.getExternalStorageDirectory() + File.separator + "DCIM/Camera/";
    private FrameLayout mContainer;
    private ViewPager mViewPager;
    private List<File> mFiles;
    private MyAdapter mMyAdapter;
    private TextView mTextView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        requsetPermission();
        initView();
        initData();

    }

    private void requsetPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }
    }

    private void initView() {
        mContainer = (FrameLayout) findViewById(R.id.fr_container);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mTextView = (TextView) findViewById(R.id.tv_page);
        mTextView.setAlpha(0.8f);
    }

    private void initData() {
        File file = new File(IMAGE_FLORD);
        if (file.exists() && file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if (listFiles.length > 0) {
//                mFiles = new ArrayList<>();
//                 //图片列表
//                ToastUtils.showToast(this, "正在加载图片列表...");
//                for (File listFile : listFiles) {
//                    if (listFile.getName().endsWith(".jpg")
//                            || listFile.getName().endsWith(".png"))
//                        mFiles.add(listFile);
//                }
//                ToastUtils.showToast(this, "图片列表加载完成");
//                 //视频列表
//                ToastUtils.showToast(this, "正在加载视频列表...");
//                for (File listFile : listFiles) {
//                    if (listFile.getName().endsWith(".mp4"))
//                        mFiles.add(listFile);
//                }
//                ToastUtils.showToast(this, "视频列表加载完成");
                mFiles = Arrays.asList(listFiles);
            }
        }

        /* 设置ViewPager限制个数 */
        mViewPager.setOffscreenPageLimit(3);

        /* 父布局触摸事件交给Viewpager处理 */
        mContainer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return mViewPager.dispatchTouchEvent(motionEvent);
            }
        });

        /* 添加滑动监听,滑动完成后设置高斯模糊 */
        mViewPager.addOnPageChangeListener(new PageChangeListener(this, mFiles, mViewPager, mContainer, this));

        /* 设置Viewpager在屏幕的位置 */
        setViewpagerParams();

        /* 设置Viewpager的动画 */
        mViewPager.setPageTransformer(true, new MyTransformation());

        mMyAdapter = new MyAdapter(this, mViewPager, mFiles, this);
        mViewPager.setAdapter(mMyAdapter);
    }



    private void setViewpagerParams() {
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        int pixels = getResources().getDisplayMetrics().widthPixels;
        lp.setMargins(pixels / 5, pixels / 13, pixels / 5, pixels / 13);
        mViewPager.setLayoutParams(lp);
    }

    @Override
    public void onItemClicked(View view) {
        int currentItem = mViewPager.getCurrentItem();
        Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(this, view, Constants.TRANSITION_NAME).toBundle();
        String filePath = mFiles.get(currentItem).getAbsolutePath();
        Intent intent = null;
        if (filePath.endsWith(".mp4")) {
            intent = new Intent(this, VideoViewerActivity.class);
            intent.putExtra(Constants.RES_IMAGE, filePath);
            startActivity(intent);
            overridePendingTransition(R.anim.video_player_zoom_enter, R.anim.video_thumbnail_zoom_enter);
        } else {
            intent = new Intent(this, ImageViewerActivity.class);
            intent.putExtra(Constants.RES_IMAGE, filePath);
            ActivityCompat.startActivity(this, intent, bundle);
        }
    }

    @Override
    public void onPageChange() {
        mTextView.setText((mViewPager.getCurrentItem() + 1) + "/" + mFiles.size());
    }
}
